package com.hello;

import java.io.IOException;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.stream.App;
import com.hello.facebook.Example;
import com.hello.games.GameFactoryBuilder;
import com.hello.resources.SoundManager;


public class MainMenuScreen extends Activity {
	
	public static boolean IsDrunk = false;
	private SoundManager mSoundManager;// in order to add sound to buttons
	DrunkoidSettingData settings = DrunkoidSettingData.getInstance();
	// DrunkoidBlockCallsManager callBlocker;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWallpaper();
        IsDrunk = true;
        Intent serviceIntent = new Intent();
		serviceIntent.setAction("com.hello.Launcher.AutoLauncherService");
		getApplicationContext().stopService(serviceIntent);
		getApplicationContext().startService(serviceIntent);
		//Intent locserviceIntent = new Intent();
		//locserviceIntent.setAction("com.hello.Launcher.LocationLauncherService");
		//startService(locserviceIntent);
        
        //terminate after 6 hours
        Handler handler = new Handler();
		handler .postDelayed(new Runnable() {
			public void run() { 
			//callBlocker.stopBlockingCalls();
				IsDrunk = false;
				finish();
			} 
		}, 6 * 60 * 60 * 1000);
		
        setContentView(R.layout.mainapplicationscreen);
        mSoundManager = new SoundManager();
		mSoundManager.initSounds(getBaseContext());
		mSoundManager.addSound(1, R.raw.comedy_zips_pops_and_bonks);
		mSoundManager.addSound(2, R.raw.sound);
		mSoundManager.addSound(3,R.raw.piggy_bank_shake);
        
        final Button buttonemergancySms = (Button) findViewById(R.id.smsbtn);
        buttonemergancySms.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	mSoundManager.playSound(3);
            	 Intent myIntent = new Intent(view.getContext(), SensSmsScreen.class);
                 startActivityForResult(myIntent, 12);
            }
        });
        
        final Button facebookBtn = (Button) findViewById(R.id.facebookbtn);
        facebookBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	mSoundManager.playSound(3);
            	//Toast.makeText(view.getContext(), R.string.facebookLoginMsg, Toast.LENGTH_LONG).show();
        	
            	Intent myIntent = new Intent(view.getContext(), App.class);
                startActivityForResult(myIntent, 0);
            	
            	/*	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        		isFacebookEnabled = prefs.getBoolean("facebookEnabled", true);
        	
        		
        			if (isFacebookEnabled) {
        			//shareFacebook();
        		}*/
            }
        });
        
        final Button buttonCallCab = (Button) findViewById(R.id.callacabbtn);
        buttonCallCab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	mSoundManager.playSound(3);
            	TaxiAutoDialer dialer = new TaxiAutoDialer(MainMenuScreen.this);
            	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        		String taxiPhone = prefs.getString("prefsTaxi", settings.getTaxiPhone());
            	dialer.callACab(taxiPhone);
            }
        });
        
        final Button buttonContacts = (Button) findViewById(R.id.contactbtn);
        buttonContacts.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	mSoundManager.playSound(3);
            	Intent newIntent = new Intent(Intent.ACTION_DEFAULT, 
            			ContactsContract.Contacts.CONTENT_URI); 
            			startActivityForResult(newIntent, 1);
            }
        });
        
        final Button buttonPhone = (Button) findViewById(R.id.phonebtn);
        buttonPhone.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	mSoundManager.playSound(3);
            	
            	DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            	    public void onClick(DialogInterface dialog, int which) {
            	        switch (which){
            	        case DialogInterface.BUTTON_POSITIVE:
            	        	Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
                        	startActivityForResult(intent, 0);
            	            break;

            	        case DialogInterface.BUTTON_NEGATIVE:
            	            break;
            	        }
            	    }
            	};
            	AlertDialog.Builder builder = new AlertDialog.Builder(MainMenuScreen.this);
            	builder.setMessage(R.string.OnPhoneBtnPressedMsg).setPositiveButton("Yes", dialogClickListener)
            	    .setNegativeButton("No", dialogClickListener).show();
            	
            	
            }
        });
        
        final Button buttonNotDrunk = (Button) findViewById(R.id.exitbtn);
        buttonNotDrunk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	mSoundManager.playSound(3);
            	askIfDrunk(4);
            }
        });
        
	}
	
	public void askIfDrunk(int requestCode){
		Class game = GameFactoryBuilder.getInstance().getRandomGame();
		Intent myIntent = new Intent(this,game);
        startActivityForResult(myIntent,requestCode);
	}
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu){
	    	MenuInflater inflater = new MenuInflater(this);
	    	inflater.inflate(R.menu.menu, menu);
	    	return true;
	    	}
	    
	    public boolean onOptionsItemSelected (MenuItem item){
	    	switch (item.getItemId()) {
			case R.id.settings:
				Intent intent = new Intent(this,SettingScreen.class);
				startActivityForResult(intent,10);
				break;
			default:
				break;
			}
	    	return true;
	    }
	    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 4:
			if (resultCode == RESULT_OK){
				//callBlocker.stopBlockingCalls();
            	Intent intent = new Intent();
            	setResult(RESULT_OK, intent);
            	IsDrunk = false;
            	finish();
			}
			break;
		case 10:
			//settings- restart the service
			Intent serviceIntent = new Intent();
			serviceIntent.setAction("com.hello.Launcher.AutoLauncherService");
			getApplicationContext().stopService(serviceIntent);
			getApplicationContext().startService(serviceIntent);
			break;
		default:
			super.onActivityResult(requestCode, resultCode, data);
			break;
		}
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		boolean retval = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) { 
			//do nothing
		} 
		else if (keyCode == KeyEvent.KEYCODE_CALL){
			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        	    public void onClick(DialogInterface dialog, int which) {
        	        switch (which){
        	        case DialogInterface.BUTTON_POSITIVE:
        	        	Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
                    	startActivityForResult(intent, 0);
        	            break;

        	        case DialogInterface.BUTTON_NEGATIVE:
        	            break;
        	        }
        	    }
        	};
        	AlertDialog.Builder builder = new AlertDialog.Builder(MainMenuScreen.this);
        	builder.setMessage(R.string.OnPhoneBtnPressedMsg).setPositiveButton("Yes", dialogClickListener)
        	    .setNegativeButton("No", dialogClickListener).show();
		}
		else { 
			retval = super.onKeyDown(keyCode, msg);
		}
		return retval;
	}
	

	public void setWallpaper() {
		Drawable wallpaper = WallpaperManager.getInstance(this).getDrawable();
        if (wallpaper == null) {
            try {
                clearWallpaper();
            } catch (IOException e) {
              Toast.makeText(this, e.getMessage(), 3).show();
            }
        } else {
        	wallpaper.setColorFilter(R.color.gray,Mode.DARKEN);
            getWindow().setBackgroundDrawable(wallpaper);
        }
	}
	
	@Override
	public void onDestroy(){
		IsDrunk = false;
		super.onDestroy();
	}

}
